const App = getApp()
const api = require('../../../../utils/api.js');

Page({
  data: {
    address: {


      items: [

        /**
         * 
        {
          id: 1,
          name: '李小姐',
          tel: '13500220022',
          address: '东城区东四北大街',
          is_def: 1

        },
        {
          id: 2,
          name: '张先生',
          tel: '13500220022',
          address: '东城区国子监',
          is_def: 0
        }
         */
      ],
      params: {
        page: 1,
        limit: 10,
      },
      paginate: {}



    },
    prompt: {
      hidden: 1,
      icon: '../../../../image/empty.png',
      title: '还没有收货地址呢',
      text: '暂时没有相关数据',
    },
  },



  onLoad() {
    this.onPullDownRefresh()
  },





  initData() {
    this.setData({
      address: {
        items: [],
        params: {
          page: 1,
          limit: 10,
        },
        paginate: {}
      }
    })
  },

  /**去编辑收货地址页面 */
  toAddressEdit(e) {
    App.WxService.navigateTo('/pages/my/address/add/add', {
      addressId: e.currentTarget.dataset.addressid
    })


  },

  /**去增加收货地址页面 */
  toAddressAdd(e) {
    console.log(e)
    App.WxService.navigateTo('/pages/my/address/add/add')
  },




  //设置默认收货地址
  toSetMajor(e) {
    var addressId = e.target.dataset.addressid;

    const paramsData = {
      is_major_addr: 'Y',
      consumer_id: App.globalData.memberInfo.member_id,
      address_id: addressId,
    }
    //编辑收货地址接口
    const data = {
      service: 'member.consumerAddressEdit',
      params: JSON.stringify(paramsData),
      url_type: 'member',

      app_token: App.globalData.app_token,         
      security_code: App.globalData.security_code
    }


    api.reqData({
      data,
      success: (res) => {
        console.log("返回 res.data>>>" + res.data);
        var resData = res.data;
        console.log(resData);
        this.onPullDownRefresh()
      },
      fail: (res) => {
        console.log(res.data);
      }
    })


  },



  //收货地址列表
  addressList: function (e) {

    const paramsData = {
      consumer_id: App.globalData.memberInfo.member_id,
    }
    //门店收藏接口
    const data = {
      service: 'member.consumerAddressFind',
      params: JSON.stringify(paramsData),
      url_type: 'member',



      app_token: App.globalData.app_token,         
      security_code: App.globalData.security_code 
    }


    var that = this

    const address = this.data.address
    const prompt = this.data.prompt

    api.reqData({
      data,
      success: (res) => {
        console.log("返回 res.data>>>" + res.data);
        var resData = res.data;
        console.log(resData);
        address.items = resData.data
        address.paginate.total = resData.data.length
        prompt.hidden = address.items.length

        this.setData({
          address: address,
          //'prompt.hidden': cash.items.length,
          prompt: prompt,
        })

      },
      fail: (res) => {
        console.log(res.data);
      },
      complete: (res) => {
        console.log("complete 返回 res>>>" + res);
      }
    })

  },





  onPullDownRefresh() {
    console.info('onPullDownRefresh')
    this.initData()
    this.addressList()
    wx.showLoading({
      title: '加载中',
    })

    setTimeout(function () {
      wx.hideLoading()
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500)
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    this.initData()
    this.addressList()

  },


  /**
   * 
  onReachBottom() {
    console.info('onReachBottom')
    if (!this.data.address.paginate.hasNext) return
    this.getList()
  },
   */
})